define([
  'vue',
  'css!' + ELMP.resource('components/file-list/index.css', 'tmzk_workflow'),
  'text!' + ELMP.resource('components/file-list/index.html', 'tmzk_workflow'),
  ELMP.resource('components/vue-collapse/index.js', 'lib'),
], function (Vue, css, template, vueCollapse) {
  return {
    components: {
      vueCollapse,
    },
    template,
    props: {
      disabled: { type: Boolean, default: false },
      header: { type: String, default: '附件' },
    },
    data() {
      return {
        fileList: [],
        isReplace: false,
        replaceId: '',
        delFileId: [],
        usersInfo: []
      }
    },
    created() { },
    mounted() {
      $.el.util.writeCss(ELMP.resource('components/upload/index.css', 'tmzk_project_project'), '#file-list')
    },
    methods: {
      // 文件大小
      fileSizeFormat(fileSize) {
        return $.el.util.fileSizeFormat(fileSize);
      },
      // 人员名称转换
      changeName(row) {
        let srt = ""
        this.usersInfo.forEach((c) => {
          if (c.id === row.userId) {
            srt = c.displayName
          }
        })
        return srt
      },
      getTableData() {
        return this.fileList.length > 0 ? this.fileList.map((c) => c.dirId).join(',') : ''
      },
      setFileList(ids, disabled = false) {
        this.disabled = disabled
        $.el.get({
          url: ELMP.url('/doc/v1/storage/batch/info'),
          data: { ids },
          success: (resp) => {
            if (resp.code === '200') {
              this.usersInfo = resp.res.users
              this.fileList = resp.res.data.map((item) => {
                return {
                  dirId: item.id,
                  uploadTime: item.updateTime,
                  fileName: item.name,
                  fileSize: item.size,
                  userId: item.updateBy
                }
              })
            }
          },
          error: (error) => {
            console.log(error)
          },
        })
      },
      onChange(file) {
        const that = this
        $.msg.loading('附件上传中')
        const formdata = new FormData()
        formdata.append('file', file.raw)
        formdata.append('objectId', '')
        formdata.append('type', '0')
        $.el.post({
          url: ELMP.url('workflow/v1/procinst/attachment/upload'),
          contentType: false,
          processData: false,
          data: formdata,
          success: (r) => {
            if (r && r.code == 200 && r.res) {
              that.usersInfo = r.res.users
              $.msg.close()
              if (that.isReplace) {
                $.msg.success('附件替换成功！')
                let index = null
                that.fileList.map((c, i) => {
                  if (c.dirId === this.replaceId) {
                    index = i
                  }
                })
                that.fileList[index] = r.res.data[0]
                that.$set(that.fileList, index, r.res.data[0])
              } else {
                $.msg.success('附件上传成功！')
                that.fileList.push(r.res.data[0])
              }
            }
            that.isReplace = false
          },
          error: (e) => {
            $.msg.close()
            $.msg.error(e.message)
          },
        })
      },
      download(row) {
        $.el.download(ELMP.url('workflow/v1/task/download/{0}', row.dirId || row.id));
      },
      exchange(row) {
        this.isReplace = true
        this.replaceId = row.dirId
        this.$refs['upload'].$children[0].$refs.input.click()
      },
      deleteRow(row) {
        const that = this
        $.msg.confirm($.i18n.get('确定删除吗!'), function () {
          // $.el.del({
          //   url: ELMP.delUrl('/doc/v1/attachment/delete?id=' + row.id),
          //   data: { id: row.id },
          //   success: (r) => {
          //     if (r && r.code === '200') {
          that.fileList = that.fileList.filter((c) => c.dirId !== row.dirId)
          $.msg.success('删除成功！')
          //     }
          //   },
          // })
        })
      },
    },
  }
})
